﻿using System.Xml.Linq;
using Microsoft.EntityFrameworkCore;
using StudentManagementSystem.Entities;

namespace StudentManagementSystem.DatabaseConn
{
    public class MysqlDbContext : DbContext
    {
        public MysqlDbContext(DbContextOptions<MysqlDbContext> options) 
            : base(options)
        {

        }

        public DbSet<Company> Companies { get; set; }
        public DbSet<Employee> Employees { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Company>()
                .Property(x => x.Name).IsRequired().HasMaxLength(100);

            modelBuilder.Entity<Company>()
                .Property(x => x.Introduction).HasMaxLength(500);


            modelBuilder.Entity<Employee>()
                .Property(x => x.EmployeeNo).IsRequired().HasMaxLength(10);

            modelBuilder.Entity<Employee>()
                .Property(x => x.FirstName).IsRequired().HasMaxLength(50);

            modelBuilder.Entity<Employee>()
                .Property(x => x.LastName).IsRequired().HasMaxLength(50);

            modelBuilder.Entity<Employee>()
                .HasOne(x => x.Company)
                .WithMany(x => x.Employees)
                .HasForeignKey(x => x.CompanyId)
                .OnDelete(DeleteBehavior.Restrict);

            var microsoftId = Guid.Parse("bbdee09c-089b-4d30-bece-44df5923716c");
            var googleId = Guid.Parse("6fb600c1-9011-4fd7-9234-881379716440");
            var alipapaId = Guid.Parse("5efc910b-2f45-43df-afae-620d40542853");

            modelBuilder.Entity<Company>().HasData(
                new Company
                {
                    Id = microsoftId,
                    Name = "Microsoft",
                    Introduction = "Great Company"
                },
                new Company
                {
                    Id = googleId,
                    Name = "Google",
                    Introduction = "Don't be evil"
                },
                new Company
                {
                    Id = alipapaId,
                    Name = "Alipapa",
                    Introduction = "Fubao Company"
                }
            );

            // Seed Employees
            modelBuilder.Entity<Employee>().HasData(
                // Microsoft Employees (already existed)
                new Employee
                {
                    Id = Guid.Parse("a1b2c3d4-e5f6-7890-a1b2-c3d4e5f67890"),
                    CompanyId = microsoftId,
                    EmployeeNo = "G003",
                    FirstName = "Mary",
                    LastName = "King",
                    DateOfBirth = new DateTime(1986, 11, 4),
                    Gender = Gender.女
                },
                new Employee
                {
                    Id = Guid.Parse("b2c3d4e5-f6a7-8901-b2c3-d4e5f6a78901"),
                    CompanyId = microsoftId,
                    EmployeeNo = "G097",
                    FirstName = "Kevin",
                    LastName = "Richardson",
                    DateOfBirth = new DateTime(1977, 4, 6),
                    Gender = Gender.男
                },

                // Google Employees
                new Employee
                {
                    Id = Guid.Parse("c3d4e5f6-a7b8-9012-c3d4-e5f6a7b89012"),
                    CompanyId = googleId,
                    EmployeeNo = "GOO1001",
                    FirstName = "Sundar",
                    LastName = "Pichai",
                    DateOfBirth = new DateTime(1972, 7, 10),
                    Gender = Gender.男
                },
                new Employee
                {
                    Id = Guid.Parse("d4e5f6a7-b8c9-0123-d4e5-f6a7b8c90123"),
                    CompanyId = googleId,
                    EmployeeNo = "GOO1002",
                    FirstName = "Lily",
                    LastName = "Wu",
                    DateOfBirth = new DateTime(1985, 3, 15),
                    Gender = Gender.女
                },

                // Alipapa Employees
                new Employee
                {
                    Id = Guid.Parse("e5f6a7b8-c9d0-1234-e5f6-a7b8c9d01234"),
                    CompanyId = alipapaId,
                    EmployeeNo = "ALI2001",
                    FirstName = "Jack",
                    LastName = "Ma",
                    DateOfBirth = new DateTime(1964, 9, 10),
                    Gender = Gender.男
                },
                new Employee
                {
                    Id = Guid.Parse("f6a7b8c9-d0e1-2345-f6a7-b8c9d0e12345"),
                    CompanyId = alipapaId,
                    EmployeeNo = "ALI2002",
                    FirstName = "Cathy",
                    LastName = "Chen",
                    DateOfBirth = new DateTime(1980, 12, 20),
                    Gender = Gender.女
                }
            );
        }
    }
}



